package org.apache.ocean.solr.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermDocs;
import org.apache.lucene.index.TermEnum;
import org.apache.lucene.search.FieldCache.StringIndex;
import org.apache.lucene.store.Directory;

public class IndexUtil {
	
	public static int getTermDoc(Term term, IndexReader indexReader) throws IOException {
		TermDocs docs = indexReader.termDocs(term);
    try {
      if (docs.next()) {
      	return docs.doc();
      }
    } finally {
      docs.close();
    }
    return -1;
	}
	
	public static List<Integer> getTermDocs(Term term, IndexReader indexReader) throws IOException {
		List<Integer> list = new ArrayList<Integer>();
		TermDocs docs = indexReader.termDocs(term);
    try {
      while (docs.next()) {
      	list.add(docs.doc());
      }
    } finally {
      docs.close();
    }
    return list;
	}
	
	public static long getSize(Directory directory) throws IOException {
		long total = 0;
		for (String file : directory.list()) {
			total += directory.fileLength(file);
		}
		return total;
	}
}
